home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / JAVAQ&A.ASC < prev    next >
Text File  |  1997-06-20  |  2KB  |  90 lines

  1. _Java Q&A_
  2. by Cliff Berg
  3.  
  4. // DirectoryLister.java
  5. public class DirectoryLister extends java.applet.Applet
  6.     implements java.awt.event.ActionListener
  7. {
  8.     private java.awt.TextField tf;
  9.     private java.awt.TextArea ta;
  10.     private java.awt.Button b;
  11.     String[] files;
  12.     
  13.     public static void main(String[] args)
  14.     {
  15.         java.awt.Frame f = new java.awt.Frame();
  16.         f.setSize(400, 220);
  17.         f.show();
  18.  
  19.         DirectoryLister dl = new DirectoryLister();
  20.         f.add("Center", dl);
  21.         dl.init();
  22.         dl.start();
  23.     }
  24.     
  25.     public void init()
  26.     {
  27.         setLayout(null);
  28.         ta = new java.awt.TextArea();
  29.         add(ta);
  30.         ta.setSize(300, 100);
  31.         ta.setLocation(10, 10);
  32.  
  33.         tf = new java.awt.TextField();
  34.         add(tf);
  35.         tf.setSize(300, 20);
  36.         tf.setLocation(10, 124);
  37.         
  38.         b = new java.awt.Button("Display");
  39.         add(b);
  40.         b.setSize(100, 20);
  41.         b.setLocation(10, 154);
  42.         
  43.         b.addActionListener(this);
  44.  
  45.         // Attempt to get the current directory
  46.         
  47.         String dir = System.getProperty("user.dir");
  48.         getListing(dir);
  49.         displayListing();
  50.     }
  51.  
  52.     public void actionPerformed(java.awt.event.ActionEvent event)
  53.     {
  54.         Object source = event.getSource();
  55.         
  56.         if (source == b)
  57.         {
  58.             System.out.println("button clicked");
  59.             // Obtain listing for the specified directory
  60.             
  61.             getListing(tf.getText());
  62.             
  63.             // Display the listing in the text window
  64.             
  65.             displayListing();
  66.         }
  67.     }
  68.     
  69.     protected void getListing(String dir)
  70.     {
  71.         java.io.File f = new java.io.File(dir);
  72.         files = f.list();
  73.     }
  74.     
  75.     protected void displayListing()
  76.     {
  77.         ta.setText("");
  78.         for (int i = 0; i < files.length; i++)
  79.         {
  80.             ta.append(files[i]);
  81.             ta.append("\r\n");
  82.         }
  83.     }
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.